home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 38 / giftif.zip / RLETIF.C < prev    next >
C/C++ Source or Header  |  1989-11-22  |  4KB  |  159 lines

  1. /*----------------------------------------------------------------------*/
  2. /* Copyright (c) 1988-1989                        */
  3. /* by CompuServe Inc., Tucson, AZ.  All Rights Reserved            */
  4. /* RLETIF.C can be copied and distributed freely for any        */
  5. /* non-commercial purposes. RLETIF.C can only be incorporated        */
  6. /* into commercial software with the permission of CompuServe Inc.    */
  7. /*----------------------------------------------------------------------*/
  8.  
  9. /* RLETIF.C */
  10.  
  11. /*
  12.  * ABSTRACT:
  13.  *    This file contains routines to convert Run Length Encoded lines
  14.  *     into one-dimensional group-3 FAX compressed lines.
  15.  *
  16.  *    See RLETIF.H for a summary of functions.
  17.  *
  18.  * AUTHOR: Doug Chinnock
  19.  *
  20.  * REVISION HISTORY:
  21.  *
  22.  */
  23.  
  24.  
  25.  
  26. #include    "cnvhuf.h"
  27. #include    "rletif.h"
  28. #include    "writetif.h"
  29.  
  30.  
  31. /* The five constant tables have the form:
  32. |
  33. |    huffman_code
  34. |        array_xxx_yyy[ n ];
  35. |
  36. |    where n is the length of the run or 1/64th of it.
  37. */
  38.  
  39. #include    "dehuftab.h"
  40.  
  41. /* Variables used among functions: */
  42.  
  43. static union
  44.     {
  45.     unsigned char
  46.     TIF_ch[4];        /* Assembly buffer for packed bits */
  47.     short int
  48.     TIF_sh[2];
  49.     long int
  50.     TIF_lg;            /* Residue always left justified here */
  51.     } TIF_pack;
  52.  
  53. short int
  54.     TIF_bit_count,
  55.     TIF_pack_filled;        /* Bits filled in TIF_pack */
  56.  
  57. extern void    compress_huffman_line( rle_line    *emit_line )
  58. {
  59. (*emit_line).rle_color = WHITE;
  60. TIF_bit_count = 0,
  61. TIF_pack_filled = 0;
  62.  
  63. TIF_write_bits( 12, 0x1 );            /* Output EOL (sync?) */
  64.  
  65. for ( (*emit_line).rle_pointer = 0;
  66.       (*emit_line).rle_pointer < (*emit_line).rle_count;
  67.       (*emit_line).rle_pointer ++ )
  68.     {
  69.     compress_huffman_code( (*emit_line).rle_color,
  70.                (*emit_line).rle_list[(*emit_line).rle_pointer] );
  71.     (*emit_line).rle_color = ! (*emit_line).rle_color;
  72.     };
  73.  
  74. TIF_write_bits( 16 - TIF_pack_filled, 0 );    /* Even out bits to even byte */
  75.  
  76. while ( TIF_bit_count < FAX_min_line )        /* Now pad line as needed */
  77.     TIF_write_bits( 8, 0 );
  78.  
  79. } /* compress_huffman_line */
  80.  
  81.  
  82. extern void     compress_huffman_code( colors    color_of_run,
  83.                        int    length_of_color )
  84. {
  85.  
  86. /* First put out makeup codes */
  87.  
  88. while ( length_of_color > 63 )
  89.     {
  90.     if ( length_of_color <= (1728+63) )
  91.     {
  92.     if ( color_of_run == WHITE )
  93.         TIF_write_bits( huf_wht_makeup[length_of_color/64-1].code_bits,
  94.                 huf_wht_makeup[length_of_color/64-1].code_value );
  95.     else
  96.         TIF_write_bits( huf_blk_makeup[length_of_color/64-1].code_bits,
  97.                 huf_blk_makeup[length_of_color/64-1].code_value );
  98.     length_of_color %= 64;
  99.     }
  100.     else if ( length_of_color <= (2560+63) )
  101.     {
  102.     TIF_write_bits( huf_big_makeup[(length_of_color-1792)/64].code_bits,
  103.             huf_wht_makeup[(length_of_color-1792)/64].code_value );
  104.     length_of_color %= 64;
  105.     }
  106.     else
  107.     {
  108.     TIF_write_bits( huf_big_makeup[(2560-1792)/64].code_bits,
  109.             huf_wht_makeup[(2560-1792)/64].code_value );
  110.     length_of_color -= 2560;
  111.     };
  112.     };
  113.  
  114. /* Finally put out termination code */
  115.  
  116. if ( color_of_run == WHITE )
  117.     TIF_write_bits( huf_wht_final[length_of_color].code_bits,
  118.             huf_wht_final[length_of_color].code_value );
  119. else
  120.     TIF_write_bits( huf_blk_final[length_of_color].code_bits,
  121.             huf_blk_final[length_of_color].code_value );
  122.  
  123. } /* compress_huffman_code */
  124.  
  125. void    TIF_write_bits( short int    size_to_write,
  126.             unsigned int    bit_value )
  127. {
  128. short int
  129.     i;
  130.  
  131. bit_value <<= 16 - size_to_write;        /* Left justify new data */
  132.  
  133. for ( i = 0; i < size_to_write; i ++ )
  134.     {
  135.     TIF_pack.TIF_lg >>= 1;
  136.  
  137.     if ( (bit_value & 0x8000) != 0 )
  138.     TIF_pack.TIF_ch[3] |= 0x80;
  139.     else
  140.     TIF_pack.TIF_ch[3] &= 0x7F;
  141.  
  142.     bit_value <<= 1;
  143.     };
  144.  
  145. TIF_pack_filled += size_to_write;
  146. TIF_pack.TIF_lg >>= 32 - TIF_pack_filled;    /* Right justify result */
  147.  
  148. while ( TIF_pack_filled >= 8 )            /* Output any full bytes */
  149.     {
  150.     put_TIF_byte( TIF_pack.TIF_ch[0] );
  151.     TIF_pack.TIF_lg >>= 8;
  152.     TIF_pack_filled -= 8;
  153.     TIF_bit_count += 8;
  154.     };
  155.  
  156. TIF_pack.TIF_lg <<= 32 - TIF_pack_filled;    /* Left justify remainder */
  157.  
  158. } /* TIF_write_bits */
  159.